package com.wlps.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlps.contant.Contant;
import com.wlps.entity.CommodityWarehouse;
import com.wlps.entity.SysRole;
import com.wlps.entity.SysUser;
import com.wlps.mapper.SysRoleMapper;
import com.wlps.mapper.SysUserMapper;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.ISysRoleService;
import com.wlps.vo.RoleParm;
import com.wlps.vo.UserParm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表(sys_role) 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-30
 */
@RestController
@RequestMapping("api/role")
@Slf4j
public class SysRoleController {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    ISysRoleService roleService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "addRole", method = RequestMethod.GET)
    public ResultVo addRole(@RequestParam Long uid, @RequestParam Long rid) {
        boolean b = sysRoleMapper.addRole(uid, rid);
        if (b) {
            UserUpdateCache();
            return ResultUtils.success("分配角色成功");
        } else {
            return ResultUtils.error("分配角色失败", 50001);
        }
    }

    @RequestMapping(value = "updateRole", method = RequestMethod.GET)
    public ResultVo updateRole(@RequestParam Long uid, @RequestParam Long rid) {
        boolean b = sysRoleMapper.updateRole(uid, rid);
        if (b) {
            UserUpdateCache();
            return ResultUtils.success("分配角色成功");
        } else {
            return ResultUtils.error("分配角色失败", 50001);
        }
    }

    /**
     * 创建角色
     *
     * @return
     */
    @RequestMapping(value = "createRole", method = RequestMethod.POST)
    public ResultVo createRole(@RequestBody SysRole role) {
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.lambda().eq(SysRole::getName, role.getName());
//查询用户是否存在
        SysRole one = roleService.getOne(query);
        if (one != null) {
            return ResultUtils.error("角色名称已经存在!");
        }

        boolean b = roleService.save(role);
        if (b) {

            RoleUpdateCache();
            return ResultUtils.success("创建角色成功");
        } else {
            return ResultUtils.error("创建角色失败");
        }
    }
    //查询角色列表
    @RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
    public ResultPageVo<List<SysRole>> getRoleList(@RequestBody RoleParm parm) {

        ResultPageVo<List<SysRole>> resultPageVo = roleService.selectRole(parm);

        return resultPageVo;
    }

    /**
     * 编辑用户保存
     *
     * @return
     */
    @RequestMapping(value = "updateSaveRole", method = RequestMethod.POST)
    public ResultVo updateSaveRole(@RequestBody SysRole role) {
//判断角色是否存在
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.lambda().eq(SysRole::getName, role.getName());
        SysRole one = roleService.getOne(query);
        Long id = one.getId();//查询出来的id
        Long editId = role.getId();//编辑的用户id
        if (!id.equals(editId)) {
            return ResultUtils.error("角色名称已存在!");
        }
        boolean b = roleService.updateById(role);
        if (b) {
            RoleUpdateCache();
            return ResultUtils.success("编辑成功");
        } else {
            return ResultUtils.error("编辑失败");
        }
    }

    @Async
    public void RoleUpdateCache() {
        List<SysRole> sysRoleList = sysRoleMapper.selectList(null);
        redisTemplate.opsForValue().set(Contant.ROLES_KEY, sysRoleList);

        log.info("所有角色-------更新缓存数据");
    }

    /**
     * 更新缓存
     */
    @Async
    public void UserUpdateCache() {
        List<SysUser> sysUserList = sysUserMapper.selectUsers();
        redisTemplate.opsForValue().set(Contant.USERS_KEY, sysUserList);

        log.info("所有用户-------更新缓存数据");
    }
}
